/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.entity.component.effects;

import org.chaoticengine.cgll.factory.EffectFactory;
import org.chaoticengine.cgll.entity.component.Component;
import org.chaoticengine.cgll.exceptions.LoadingException;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

/**
 * Component to render a Slick2D particle effect attached to an entity.
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
@Root
public class ParticleEffectComponentLoader extends Component {
    protected String name = "";
    protected ParticleEffectComponent pec = null;

    @Element(name="system")
    public String getName() {
        return name;
    }

    @Element(name="system")
    public void setName(String name) {
        this.name = name;

        try {
            pec = EffectFactory.getInstance().getComponent(name);
        } catch (LoadingException lEx) {
            System.out.println("Error: Failed to load particle system '" + name + "'.");
            lEx.printStackTrace();
            System.exit(-1);
        }
    }

    @Override
    public void onSetOwner() {
        if (pec != null) {
            getOwner().getComponentManager().addComponent(pec);
        }

        getOwner().getComponentManager().removeComponent(this);
    }
}
